package pers.jassor.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import pers.jassor.entities.Dept;

@RestController
public class DeptControllerConsumer {
	
	// private static final String REST_URL_PREFIX = "http://localhost:8001";
	
	private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-DEPT";
	
	// 用于提供访问远程Http服务的方法
	// 简单便捷的访问restful服务模板类
	// 是Spring提供的用于访问Rest服务的客户端模板工具集
	@Autowired
	private RestTemplate restTemplate;
	
	@PostMapping("/consumer/dept")
	public boolean add(Dept dept) {
		return restTemplate.postForObject(REST_URL_PREFIX+"/dept", dept, Boolean.class);
	}
	
	@GetMapping("/consumer/dept/{id}")
	public Dept get(@PathVariable("id") Long id) {
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/"+id, Dept.class);
	}

	@SuppressWarnings("unchecked")
	@GetMapping("/consumer/dept")
	public List<Dept> get() {
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/", List.class);
	}
	
	@GetMapping("/consumer/dept/discovery")
	public Object dicovery() {
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/discovery", Object.class);
	}
	
}
